import axios from 'axios'
import store from '@/store/index.js'

const instance = axios.create({
  baseURL: 'http://ttapi.research.itcast.cn'
})

instance.interceptors.request.use(config => {
  // 登陆后就有了token，有token就加在请求头中
  if (store.state.userToken !== null) {
    config.headers.Authorization = `Bearer ${store.state.userToken.token}`
  }
  // 在发送请求之前做些什么
  return config
}, error => {
  // 对请求错误做些什么
  return Promise.reject(error)
})

export default instance
